package com.apesay.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apesay.service.base.ClientService;
import com.apesay.service.work.ProjectService;
import com.apesay.service.work.TaskService;
import com.apesay.utils.Constant;
import com.apesay.utils.DataUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/task")
public class TaskController extends RestController{
	@Autowired
	private TaskService taskService;
	@Autowired
	private ClientService clientService;
	@Resource
	private HttpSession session;
	
	@RequiresAuthentication
	@RequestMapping("/manage")
	public String manage(){
		return "task/manage";
	}
	
	@RequiresAuthentication
	@RequestMapping("/add")
	@ResponseBody
	public JSONObject add(String boardName,String proId,Integer visibility){
		JSONObject userObj = (JSONObject) session.getAttribute("currentaccount");//当前登录用户
		boolean testAdd = false;
		String username = userObj.getString("username");
		if ("test".equals(username)) {
			testAdd = true;
		}
		JSONObject model = new JSONObject();
		JSONObject data = new JSONObject();
		if ("0".equals(proId)) {
			if (testAdd) {
				addTask(boardName, proId, visibility, userObj, model, data);
			}
		} else {
			addTask(boardName, proId, visibility, userObj, model, data);
		}
		return model;
	}

	@RequiresAuthentication
	@RequestMapping("/edit")
	public String edit(String objectId,Model model){
		JSONObject task = taskService.get(objectId);
		model.addAttribute("task", task);
		return "task/edit";
	}
	
	public JSONObject save(@RequestBody String body){
		return taskService.save(JSON.parseObject(body));
	}
	
	public JSONObject update(@PathVariable String id,@RequestBody String body){
		Object userObj = session.getAttribute("currentaccess");//当前登录用户
		JSONObject reObject = new JSONObject();
		if (DataUtils.isNotEmpty(userObj)) {
			JSONObject jsonObject = JSON.parseObject(body);
			reObject = taskService.update(jsonObject.getString(Constant.OBJECTID), jsonObject);
		}
		return reObject;
	}
	
	public JSONObject delete(@PathVariable String id){
		return taskService.del(id);
	}

	private void addTask(String boardName, String proId, Integer visibility, JSONObject userObj, JSONObject model, JSONObject data) {
		String userId = userObj.getString(Constant.OBJECTID);
		JSONObject jsonObject = new JSONObject();
		JSONArray workers = new JSONArray();
		workers.add(userId);
		jsonObject.put("creater",userId);
		jsonObject.put("boardName",boardName);
		jsonObject.put("proId",proId);
		jsonObject.put("visibility",visibility);
		jsonObject.put("workers",workers);
		JSONObject save = taskService.save(jsonObject);
		data.put("data",save);
		model.put("succeed", 1);
		model.put("message", data);
	}
	
	@Override
	protected String getModel() {
		return Constant.model_task;
	}
}
